package com.parking.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.parking.common.core.model.RedisKey;
import com.parking.common.core.util.DateUtil;
import com.parking.common.web.util.RedisUtil;
import com.parking.order.dao.PlatformIncomeDao;
import com.parking.order.model.CentifiedIncome;
import com.parking.order.dao.CentifiedIncomeDao;
import com.parking.order.model.PlatformIncome;
import com.parking.order.model.dto.FindCommentAndOrderDto;
import com.parking.order.service.ICentifiedIncomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.order.util.BasicInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2023年11月28日
 */
@Service
public class CentifiedIncomeServiceImpl extends ServiceImpl<CentifiedIncomeDao, CentifiedIncome> implements ICentifiedIncomeService {
    /**
     * Redis工具类
     * */
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 物业收入数据层
     * */
    @Autowired
    private CentifiedIncomeDao certifiedIncomeDao;
    /**
     * 其他模块调用工具类
     * */
    @Autowired
    private BasicInfoUtil basicInfoUtil;
    /**
     * 物业查询12月每日收入
     * 参数 物业账号Id certifiedId
     * 返回 List<Integer> 1-31日 收入 BigDecimal->int
     * */
    @Override
    public List<Integer> findDailyIncomeCertified(Integer certifiedId) throws Exception {
        System.out.println(certifiedId);

        List<Integer> incomes = new ArrayList<>();
        Integer certifiedInfoId = basicInfoUtil.getCertifiedAccount(certifiedId).getCertifiedInfoId();

        System.out.println(certifiedInfoId);
        
        for(int i=1;i<=31;i++){
            // 返回12月1日的收入 BigDecimal->Int
            String day = "2023-12-" + (i<10?"0"+i:i);
            System.out.println(day);
            CentifiedIncome certified = certifiedIncomeDao.selectOne(new QueryWrapper<CentifiedIncome>().eq("year_month_time",day).eq("certified_info_id",certifiedInfoId));

            if(certified==null){
                System.out.println("null");
            }else{
                System.out.println("不是null,收入为:"+certified.getIncome());
            }

            int income = certified==null?0:certified.getIncome().intValue();
            if(DateUtil.localDateToString(LocalDate.now()).equals(day)){
                System.out.println(day+"是今天");
                // 当天还要算Redis
                if(redisUtil.isZSetMember(RedisKey.certifiedIncome(), String.valueOf(certifiedInfoId))){
                    System.out.println("当天前收入:"+income);
                    income += (int) redisUtil.ZSetScore(RedisKey.certifiedIncome(), String.valueOf(certifiedInfoId));
                    System.out.println("当天后收入:"+income);
                }
            }
            incomes.add(income);
        }

        return incomes;
    }

    /**
     * 物业查询12月差评未回数与超时未离开订单数
     * 参数 certifiedId 物业Id
     * 返回 badComment 差评未回数
     *     shEndOrder 超时未离开订单数
     * */
    @Override
    public FindCommentAndOrderDto findCommentAndOrder(Integer certifiedAccountId) throws Exception {
        Integer infoId = basicInfoUtil.getCertifiedAccount(certifiedAccountId).getCertifiedInfoId();
        int badComment = 0;
        int shEndOrder = 0;

        if(redisUtil.exist(RedisKey.certifiedBadComment(infoId))){
            badComment += (int) Double.parseDouble(redisUtil.get(RedisKey.certifiedBadComment(infoId)));
        }
        if(redisUtil.exist(RedisKey.certifiedOrderOvertime(infoId))){
            shEndOrder += (int) Double.parseDouble(redisUtil.get(RedisKey.certifiedOrderOvertime(infoId)));
        }

        return new FindCommentAndOrderDto(badComment,shEndOrder);
    }
}
